package com.hb.trade.api.routine.task;

import cn.hutool.core.collection.CollUtil;
import com.hb.core.pojo.enums.NewsTypeEnum;
import com.hb.core.pojo.enums.NewsTypeTwoEnum;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.core.pojo.project.section.dto.BidSectionTenderDTO;
import com.hb.core.pojo.project.section.entity.BidSectionTender;
import com.hb.core.util.CronExpressionUtil;
import com.hb.trade.api.routine.util.SendNewsUtil;
import com.hb.trade.client.center.NewsCenterClient;
import com.hb.trade.client.project.SectionClient;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.msg.Message;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.ZoneId;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * describe:
 *
 * @author zhongdl
 * @date 2022/02/19
 */
@Component
@Slf4j
public class SendNewsTask {

    protected final SectionClient sectionClient;
    protected final NewsCenterClient newsCenterClient;

    public SendNewsTask(SectionClient sectionClient,
                        NewsCenterClient newsCenterClient) {
        this.sectionClient = sectionClient;
        this.newsCenterClient = newsCenterClient;
    }

    /**
     *
     * @Description: 发送系统通知消息
     * @Author: zhongdl
     * @Date:2022/2/19 10:41
     **/
    @Scheduled(cron = CronExpressionUtil.EVERY_9_CLOCK)
    public void sendSystemNotification() {
        //查询还剩3天开标的标段
        BidSectionDTO bidSectionDTO = new BidSectionDTO();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 3);
        bidSectionDTO.setOpenBidTime(cal.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        //查询未开
        Message<List<BidSectionDTO>> listMessage = sectionClient.listBidSectionByOpenBidTime(bidSectionDTO);
        if (listMessage.isFail() || CollUtil.isEmpty(listMessage.getData())) {
            return;
        }
        List<BidSectionDTO> data = listMessage.getData();
        BidSectionTenderDTO bidSectionTenderDTO = new BidSectionTenderDTO();
        for (BidSectionDTO bidSection : data) {
            //查询未递交投标文件的标段
            bidSectionTenderDTO.setSectionId(bidSection.getId());
            bidSectionTenderDTO.setStatus(0);
            Message<List<BidSectionTenderDTO>> bidSectionTenderMsg = sectionClient.listBidSectionTender(bidSectionTenderDTO);
            if (bidSectionTenderMsg.isFail() || CollUtil.isEmpty(bidSectionTenderMsg.getData())) {
                continue;
            }
            List<Long> collect = bidSectionTenderMsg.getData().stream().map(BidSectionTender::getBidderId).collect(Collectors.toList());
            //发送消息
            SendNewsUtil.sendBiddereeNews(
                    bidSection.getId(),
                    NewsTypeEnum.SYSTEM_NOTIFICATION,
                    NewsTypeTwoEnum.FILE_SYSTEM_NOTIFICATION,
                    "3",
                    null,
                    "/console/bidInfo/tenderFileInfo",
                    collect
            );
        }
    }
}
